#ver 1.4 2009-5-27

element_table = {1:"H", 6:"C", 7:"N", 8:"O"} 

def keywords_gin(o, keywords, options):
    for i in keywords:
        o.write("%s "%i)
    o.write("\n\n")
    if len(options) > 0:
        for i in options:
            o.write("%s\n"%i)
        o.write("\n")
            

def option_gin():
    pass
def title_gin(o):
    o.write("""title
GULP calculation from Direct Force Field for 3D Atomistic
end
""")
    
def cell_gin(msdfile, o):
    boxcontrol = [1, 1, 1, 1, 1, 1]
    o.write("\ncell\n")
    f = open(msdfile, 'r')
    
    for i in f:
        if i.strip().startswith("PBC"):
            #["%s=%s" % (k, v) for k, v in params.items()]
            cellparameters = [float(m) for m in i.split()[1:]]
            coord ="".join(["%-11.6f" % n for n in cellparameters])
            control ="".join(["%2d" % n for n in boxcontrol])
            o.write("%s%s\n"%(coord, control))

    f.close()

def coords_gin(msdfile, o):
    atomTypeList = []
    counter = 0
    control = "1  0  1 1 1"
    
    o.write("\ncartesian\n")
    f = open(msdfile, 'r')
    eqt_table_apptype_fftype = {}
    eqt_table_fftype_apptype = {}

    for i in f:
        if len(i.split()) == 12:
            #gulp
            #O1 core 0.500000 0.500000 0.500000 -0.800000 1.000000 0.0 1 1 1
            #coord, charge,site occupancy, ion radius for a breathing shell model
            #dff
            #1      O      8    OH2          o_2w  -0.800000   10.000000    10.000000    10.000000    1    UNK       1
            #0      1      2    3            4      5           6           7             8           9    10        11
            coord_input = [float(m) for m in i.split()[6:9]]
            coord = "".join(["%11.6f" % n for n in coord_input])
            if (i.split()[3] in atomTypeList) == False:
                atomTypeList.append(i.split()[3])
                counter += 1
            if i.split()[3] in eqt_table_fftype_apptype:
                atomType = eqt_table_fftype_apptype[i.split()[3]]
            else:
                atomType = "%s%d"%(element_table[int(i.split()[2])],counter)
            eqt_table_apptype_fftype[atomType] = i.split()[3]
            eqt_table_fftype_apptype[i.split()[3]] = atomType
            o.write("%-7s%6s%31s%10.6f%12s\n"%(atomType,"core",coord,float(i.split()[5]), control))
    f.close()
   
    return eqt_table_apptype_fftype,  eqt_table_fftype_apptype

def spieces_gin(o, eqt_table):
    o.write("\nSpecies\n")
    
    for (k, v) in eqt_table.items():
        o.write("%-10score%10s\n"%(k, v))

def spacegroup(o):
    o.write("\nspacegroup\nP 1\n\n")

def connect_gin(msdfile, o):
    f = open(msdfile, 'r')

    for i in f:
        control = ["single", 0, 0, 0]
        if len(i.split()) == 3:
            a = i.split()
        #connect 1 3 single  0 0 0
            o.write("connect%5d%5d%7s%2d%2d%2d\n"%(int(a[0]), int(a[1]),control[0], control[1], control[2], control[3]))

    f.close()

def lib_gin(lib, o):
    o.write("\nlibrary%20s\n"%lib)

def out_gin(o):
    pass

def  msd2gin(ginfile, msdfile, keywords, options, lib, fit):

    if fit == 0:
        o = open(ginfile, 'w')
    
        keywords_gin(o, keywords, options)
        title_gin(o)
        cell_gin(msdfile, o)
        eqt_table_apptype_fftype, eqt_table_fftype_apptype = coords_gin(msdfile, o)
        spacegroup(o)
        connect_gin(msdfile, o)
        lib_gin(lib, o)

        return eqt_table_fftype_apptype
    else:
        o = open(ginfile, 'w')
    
        keywords_gin(o, keywords, options)
        title_gin(o)
        cell_gin(msdfile, o)
        eqt_table_apptype_fftype, eqt_table_fftype_apptype = coords_gin(msdfile, o)
        spacegroup(o)
        connect_gin(msdfile, o)

        return eqt_table_fftype_apptype
        
if __name__ == "__main__":
    msd2gin(ginfile, msdfile, keywords, lib, fit)
    
